package com.cuitrip.business.location.map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.services.core.ServiceSettings;
import com.cuitrip.service.R;
import com.cuitrip.util.I118Util;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;

import java.util.Locale;

/**
 * Created by MiJack on 2015/12/13.
 */
public class GaodeMapShowFragment extends CustomUiFragment {
    MapView mapView;
    private AMap map;
    private double lat;
    private double lng;
    private String name;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.layout_gaodemap);
        mapView = (MapView) view.findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        map = mapView.getMap();
        ServiceSettings.getInstance().setLanguage(I118Util.getLocale().getLanguage().equals(Locale.ENGLISH.getLanguage()) ? ServiceSettings.ENGLISH : ServiceSettings.CHINESE);
        if (readArguments()) {
            MarkerOptions options = new MarkerOptions();
            options.title(name);
            LatLng latlng = new LatLng(lat, lng);
            options.position(latlng);
            map.addMarker(options);
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));
        }
        return view;
    }

    public boolean readArguments() {
        if (getArguments().containsKey(GaoDeMapActivity.VALUE_LAT) &&
                getArguments().containsKey(GaoDeMapActivity.VALUE_LNG) && getArguments().containsKey(GaoDeMapActivity.VALUE_NAME)) {
            lat = getArguments().getDouble(GaoDeMapActivity.VALUE_LAT, 0);
            lng = getArguments().getDouble(GaoDeMapActivity.VALUE_LNG, 0);
            name = getArguments().getString(GaoDeMapActivity.VALUE_NAME);
            return true;
        }
        return false;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public void onTopbarLeftPress() {
        getActivity().finish();
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mRightStyle = CustomUiConfig.AreaStyle.ICON_STYLE;
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = getString(R.string.trip_attribute_meet);
        return config;
    }
}
